import {ActionContext, Module} from 'vuex';
import OU from '@/store/entities/administration/organization-unit/o-u';
import Ajax from '@/lib/ajax';
import User from '@/store/entities/administration/user/user';
import PageResult from '@/store/entities/page-result';

interface ListState<T> {
    list: T[];
    loading:boolean;
    currentOU: any;
    addedUsers: User[];
    addedCount: number;
    addedCurrentPage: number;
    addedPageSize: number;
    pagedUsers: any;
}

class OUModule implements Module<ListState<OU>, any> {
    namespaced = true;
    state = {
        list: new Array<OU>(),
        loading:false,
        currentOU: {},
        addedUsers: new Array<User>(),
        addedCount: 0,
        addedCurrentPage: 1,
        addedPageSize: 10,
        pagedUsers: {},
    };
    actions = {
        async getOrganizationUnits(context: ActionContext<ListState<OU>, any>, payload: any) {
            const response = await Ajax.get('/api/services/app/OrganizationUnit/GetOrganizationUnits');
            context.state.list = response.data.result.items;
        },
        async createOrganizationUnit(context: ActionContext<ListState<OU>, any>, payload: any) {
            const response = await Ajax.post('/api/services/app/OrganizationUnit/CreateOrganizationUnit', payload.data);
            context.state.currentOU = response.data.result;
            context.state.list.push(response.data.result as OU);
        },
        async updateOrganizationUnit(context: ActionContext<ListState<OU>, any>, payload: any) {
            const response = await Ajax.put('/api/services/app/OrganizationUnit/UpdateOrganizationUnit', payload.data);
            context.state.currentOU = response.data.result;
            const index = context.state.list.findIndex((item) => {
                return item.id === payload.data.id;
            });
            context.state.list.splice(index, 1, response.data.result as OU);

        },
        async deleteOrganizationUnit(context: ActionContext<ListState<OU>, any>, payload: any) {
            await Ajax.delete('/api/services/app/OrganizationUnit/DeleteOrganizationUnit?Id='+payload.data.id);
            const index = context.state.list.findIndex((item) => {
                return item.id === payload.data.id;
            });
            context.state.list.splice(index, 1);
        },
        async moveOrganizationUnit(context:ActionContext<ListState<OU>, any>,payload:any){
            await Ajax.post('/api/services/app/OrganizationUnit/MoveOrganizationUnit',payload.data);
        },
        async obtainOrganizationUnitUsers(context: ActionContext<ListState<OU>, any>, payload: any) {
            context.state.loading = true;
            const response = await Ajax.post('/api/services/app/OrganizationUnit/ObtainOrganizationUnitUsers', payload.data);
            context.state.loading = false;
            const page = response.data.result as PageResult<User>;
            context.state.addedUsers = page.items;
            context.state.addedCount= page.totalCount;
        },
        async obtainOrganizationUnitPagedUsers(context:ActionContext<ListState<OU>, any>,payload:any){
            context.state.loading=true;
            payload.data.id=payload.data.connectId;
            const response = await Ajax.post('/api/services/app/OrganizationUnit/ObtainOrganizationUnitUsers', payload.data);
            context.state.loading=false;
            context.state.pagedUsers=response.data.result;
        },
        async findUsers(context: ActionContext<ListState<OU>, any>, payload: any) {
            context.state.loading=true;
            payload.data.organizationUnitId=payload.data.connectId;
            const response = await Ajax.post('/api/services/app/OrganizationUnit/FindUsers', payload.data);
            context.state.loading=false;
            context.state.pagedUsers = response.data.result;
        },
        async addUsersToOrganizationUnit(context: ActionContext<ListState<OU>, any>, payload: any) {
            await Ajax.post('/api/services/app/OrganizationUnit/AddUsersToOrganizationUnit', payload.data);
        },
        async removeUserFromOrganizationUnit(context: ActionContext<ListState<OU>, any>, payload: any) {
            await Ajax.delete('/api/services/app/OrganizationUnit/RemoveUserFromOrganizationUnit', {params: payload.data});
        },
    };
    mutations = {
        setCurrentOU(state:ListState<OU>,payload:any){
            state.currentOU=payload.ou;
        },
        setAddedCurrentPage(state: ListState<OU>, page: number) {
            state.addedCurrentPage = page;
        },
        setAddedPageSize(state: ListState<OU>, pageSize: number) {
            state.addedPageSize = pageSize;
        },
    };

}

const ouModule = new OUModule();
export default ouModule;
